package com.yd.mvc.mobile.goodsWeightController;

import com.yd.app.service.mobile.goodsWeightService.MGoodsWeightService;
import com.yd.app.service.pc.goodsWeightService.GoodsWeightService;
import com.yd.app.util.BusinessException;
import com.yd.domain.business.GoodsWeight;
import com.yd.domain.commom.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 商品规格
 * Created by sunxianhui on 2018/4/26.
 */
@RestController
@RequestMapping(value = "/mapi/goodsWeight")
public class MGoodsWeightController {
    @Autowired
    MGoodsWeightService mgoodsWeightService;

    /**
     * 添加
     * @param goodsWeight
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public GoodsWeight addGoodsWeight(@RequestBody GoodsWeight goodsWeight) throws BusinessException {
        int count= mgoodsWeightService.addGoodsWeight(goodsWeight);
        if(count==1){
            return goodsWeight;
        }else {
            throw new BusinessException(500, "添加商品规格信息失败");
        }
    }

    /**
     * 修改
     * @param goodsWeight
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public void updateGoodsWeight(@RequestBody GoodsWeight goodsWeight) {
        mgoodsWeightService.updateGoodsWeight(goodsWeight);
    }

    /**
     * 删除
     * @param id
     */
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    public void deleteGoodsWeight(@PathVariable("id") String id) {
        mgoodsWeightService.deleteGoodsWeightByid(id);
    }

    /**
     *
     */
    @RequestMapping(value = "/getListByGoodsId/{goodsId}", method = RequestMethod.GET)
    public List<GoodsWeight> getListByGoodsId(@PathVariable("goodsId") String goodsId) {
        List<GoodsWeight> goodsWeightList=new ArrayList<>();
        goodsWeightList=mgoodsWeightService.getGoodsWeight(goodsId);
        return goodsWeightList;
    }
    /**
     * 根据条件分页查询
     * @param goodsId
     * @param pageIndex
     * @param pageSize
     * @return
     * @throws BusinessException
     */
    @RequestMapping(value = "/getList/{goodsId}/{pageIndex}/{pageSize}", method = RequestMethod.GET)
    public PageModel getGoodsWeightList(@PathVariable("goodsId") String goodsId, @PathVariable("pageIndex") int pageIndex, @PathVariable("pageSize") int pageSize) throws BusinessException {
        PageModel pageModel = null;
        try {
            pageModel = mgoodsWeightService.getGoodsWeightList(goodsId, pageIndex, pageSize);
        } catch (Exception e) {
            throw new BusinessException(500, "获取商品规格信息失败");
        }
        return pageModel;
    }
}
